import {getMaterRest,getMaterNews} from '@/api/mater'
export default {
    namespace:true,
    state: {
        materData: {
            image: {
                items:[],
                count:-1
            },
            voice: {
                items:[],
                count:-1
            },
            video: {
                items:[],
                count:-1
            },
            news: {
                items:[],
                count:-1
            }
        }
    },
    mutations: {
        setMaterDataImage(state, data) {
            if (data) {
                state.materData.image = data
                window.localStorage.setItem('materData', JSON.stringify(state.materData))
            }
        },
        setMaterDataVoice(state, data) {
            if (data) {
                state.materData.voice = data
                window.localStorage.setItem('materData', JSON.stringify(state.materData))
            }
        },

        setMaterDataVideo(state, data) {
            if (data) {
                state.materData.video = data
                window.localStorage.setItem('materData', JSON.stringify(state.materData))
            }
        },

        setMaterDataNews(state, data) {
            if (data) {
                state.materData.news = data
                window.localStorage.setItem('materData', JSON.stringify(state.materData))
            }
        },
        setMaterData(state, materData) {
            if (materData) {
                state.materData = materData
                window.localStorage.setItem('materData', JSON.stringify(state.materData))
            }
        },
        /**
         * 清空素材数据
         * @param state
         */
        clearMaterData(state) {
            state.materData = {
                image: {
                    items:[],
                    count:-1
                },
                voice: {
                    items:[],
                    count:-1
                },
                video: {
                    items:[],
                    count:-1
                },
                news: {
                    items:[],
                    count:-1
                }
            }
            window.localStorage.removeItem('materData')
        },
        getInitMater(state){
            let dt = JSON.parse(window.localStorage.getItem('materData'))
            state.materData.image = dt && dt.image ? dt.image : {items:[],count:-1}
            state.materData.voice = dt && dt.voice ? dt.voice : {items:[],count:-1}
            state.materData.video = dt && dt.video ? dt.video : {items:[],count:-1}
            state.materData.news = dt && dt.news ? dt.news : {items:[],count:-1}
            return state.materData
        }
    },
    actions: {
        /**
         * 获取图片素材数据
         * @param state
         * @param commit
         * @param page      分页页数
         */
        getMaterDataImage({state,commit},page){
            return getMaterRest(page,"image")
                .then(({data}) => {
                    if(data.code === 200){
                        let dt = data.data
                        commit('setMaterDataImage',dt)
                    }
                })
        },
        /**
         * 获取音频素材消息
         * @param state
         * @param commit
         * @param page
         */
        getMaterDataVoice({state,commit},page){
            return getMaterRest(page,"voice")
                .then(({data}) => {
                    if(data.code === 200){
                        let dt = data.data
                        commit('setMaterDataVoice',dt)
                    }
                })
        },
        /**
         * 获取视频素材消息
         * @param state
         * @param commit
         * @param page
         */
        getMaterDataVideo({state,commit},page){
            return getMaterRest(page,"video")
                .then(({data}) => {
                    if(data.code === 200){
                        let dt = data.data
                        commit('setMaterDataVideo',dt)
                    }
                })
        },
        /**
         * 获取图文素材详情
         * @param state
         * @param commit
         * @param page
         */
        getMaterDataNews({state,commit},page){
            return getMaterNews(page)
                .then((res) => {
                    let data = res.data
                    if(data.code === 200){
                        let dt = data.data
                        commit('setMaterDataNews',dt)
                    }
                })
        }
    }
}